import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/login.vue'
import Layout from '@/views/layout/layout.vue'
// import Sidebar from '@/views/layout/sidebar/sidebar.vue'
// import Nav from '@/views/layout/nav/nav.vue'
// import Main from '@/views/layout/main/main.vue'
import store from '@/store'
Vue.use(VueRouter)

// 创建路由对象
const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout
      // redirect:"",重定向
      // children: [
      //   { path: '/sidebar', component: Sidebar },
      //   { path: '/nav', component: Nav },
      //   { path: '/main', component: Main }

      // ]
    }
  ]
})

// 前置导航守卫
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  // console.log(token)
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})
export default router
